package com.fmistuff.chain.nodes;

import java.util.HashMap;
import java.util.Map;

public class FactorNode extends MarkovNode {

	public static final String HASHKEY = FactorNode.class.getSimpleName();
	private static Map<String, MarkovNode> pool;
	private static final String BRACKETS = "()";

	static {
		pool = new HashMap<String, MarkovNode>();
		pool.put(EMPTY, new NumNode()); // 0.5f
		pool.put(BRACKETS, new ExprNode());
		
		pool.get(BRACKETS).setProbability(0.2f);
	}

	public FactorNode() {
		this(new NumNode().getData());
	}

	protected FactorNode(String data) {
		this(data, 0.5f);
	}

	protected FactorNode(String data, float probability) {
		super(data, probability);
	}

	@Override
	public String descend() {
		MarkovNode currentNode = pickANode(pool);

		if (!isFinal()) {
			if (currentNode instanceof ExprNode) { // if the expr node
				return "(" + currentNode.descend() + ")";
			} else { // it is a num node
				return currentNode.descend();
			}
		}

		return getData();
	}

	@Override
	public boolean isFinal() {
		return false;
	}

}
